package com.openspring.platform.service.impl;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.swing.SortOrder;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import com.openspring.framework.extend.shiro.ShiroJdbcRealm;
import com.openspring.framework.tree.Node;
import com.openspring.framework.util.ObjectUtils;
import com.openspring.platform.dao.ResourceDAO;
import com.openspring.platform.dao.SecurityDAO;
import com.openspring.platform.model.Resource;
import com.openspring.platform.service.SecurityService;

@Service("securityService")
public class SecurityServiceImpl implements SecurityService {

	private SecurityDAO securityDAO;
	private ResourceDAO resourceDAO;
	private ShiroJdbcRealm shiroJdbcRealm;
	
	@javax.annotation.Resource(name="securityDAO")
	public void setSecurityDAO(SecurityDAO securityDAO) {
		this.securityDAO = securityDAO;
	}

	@javax.annotation.Resource(name="resourceDAO")
	public void setResourceDAO(ResourceDAO resourceDAO) {
		this.resourceDAO = resourceDAO;
	}
	
	@javax.annotation.Resource(name="shiroJdbcRealm")
	public void setShiroJdbcRealm(ShiroJdbcRealm shiroJdbcRealm) {
		this.shiroJdbcRealm = shiroJdbcRealm;
	}

	@Override
	public List<Map<String, Object>> findResourceOperators(Long resourceId) {
		return securityDAO.queryResourceOperators(resourceId);
	}

	@Override
	public void saveResourceOperators(Long resourceId, Long...operatorId) {
		securityDAO.deleteResourceOperators(resourceId);
		securityDAO.insertResourceOperators(resourceId, operatorId);
		shiroJdbcRealm.clearAllCachedAuthorizationInfo();
	}
	
	@Override
	public Node getOperatorPermissionsTree(Long roleId) {
		Node root = new Node();
		root.setText(".");
		List<Resource> resources = resourceDAO.queryWhere(" WHERE status = ?", "enable");
		Map<Long, Node> map = new HashMap<Long, Node>();
		Iterator<Resource> iter = resources.iterator();
		while(iter.hasNext()) {
			Node node = new Node();
			Resource resource = iter.next();
			BeanUtils.copyProperties(resource, node);
			List<Map<String, Object>> permissions = securityDAO.queryResourceOperatorPermissions(roleId, node.getId());
			for(Map<String, Object> permission : permissions) {
				Node node1 = new Node();
				node1.setCustomId(Long.valueOf(permission.get("operatorId").toString()));
				node1.setParentId(node.getId());
				node1.setIconCls(ObjectUtils.parseNull(permission.get("iconCls")).toString());
				node1.setChecked(Boolean.valueOf(permission.get("checked").toString()));
				node1.setSort(Long.parseLong(permission.get("sort").toString()));
				node1.setText(ObjectUtils.parseNull(permission.get("text")).toString());
				node.getChildren().add(node1);
			}
			map.put(node.getId(), node);
		}
		Set<Map.Entry<Long, Node>> entrySet = map.entrySet();
		Iterator<Map.Entry<Long, Node>> iter1 = entrySet.iterator();
		while(iter1.hasNext()) {
			Node node = iter1.next().getValue();
			if(node.getParentId() == 0) {
				root.getChildren().add(node);
			} else {
				Node parentNode = map.get(node.getParentId());
				if(parentNode != null)
					parentNode.getChildren().add(node);
			}
		}
		root.sort(SortOrder.ASCENDING);
		return root;
	}

	@Override
	public List<Map<String, Object>> findOperatorPermissions(Long roleId) {
		return securityDAO.queryOperatorPermissions(roleId);
	}

	@Override
	public void saveOperatorPermissions(Long roleId, Long[] resourceIds,
			Long[] operatorIds) {
		securityDAO.deleteRoleOperatorPermissions(roleId);
		securityDAO.insertRoleOperatorPermissions(roleId, resourceIds, operatorIds);
		shiroJdbcRealm.clearAllCachedAuthorizationInfo();
	}
}
